package ui;

import javax.swing.*;

import dal.IPet;
import dal.Pet;
import dal.Pet_type;
import dal.User;
import java.awt.*;              //for layout managers and more
import java.awt.event.*;        //for action events
import java.awt.image.BufferedImage;

public class PetDetailsFrame extends JFrame{
	
	// Data members Definition
	private final int PICTURE_HEIGHT = 120;
	private final int PICTURE_WIDTH = 120;
	private JFrame callingFrame = null;
	private closeButtonHandler closebuttonHandler = new closeButtonHandler();
	private IPet selectedPet = null;
	private User user = null;
	private Pet_type petType = null;
	private PetDetailsFrame petDetailsFrame = null;
	private byte[] picture = null;
	
	// Components Definition
	private Container mainCotainer = null;
	private JPanel picturePane = new JPanel();
	private JPanel textControlsPane = new JPanel(new GridBagLayout());
	private JPanel buttonsPanel = new JPanel(new FlowLayout());
	private JTextField petNameField = new JTextField(15);
	private JLabel petNameLabel = new JLabel ("Pet's Name:");
	private JTextArea LongDesctextArea = new JTextArea(10,30);
	private JLabel longDescLabel = new JLabel ("Long Description:");
	private JTextField shortDescField = new JTextField(10);
	private JLabel shortDescLabel = new JLabel ("Short Description:");
	private JTextField deliverUserField = new JTextField(10);
	private JLabel deliverUserLabel = new JLabel ("Delivered By:");
	private JTextField petTypeField = new JTextField(10);
	private JLabel petTypeLabel = new JLabel ("Pet's Type:");
	private ImageIcon petImage = null;
	private JButton closeButton = new JButton ("Close");
	private JButton pictureButton = new JButton ();
	private JScrollPane longDescScrollPane = new JScrollPane(LongDesctextArea);
	
	private JLabel[] labels = {petNameLabel, petTypeLabel, deliverUserLabel, shortDescLabel, longDescLabel};
	private JTextField[] textFields = {petNameField, petTypeField, deliverUserField, shortDescField};

	// Constructor Definition
	public PetDetailsFrame(JFrame clientframe)
	{
		// set the title
		super("Pet's Details Window");
		
		// set parent's frame
		callingFrame = clientframe;
		
		// set reference to current frame
		petDetailsFrame = this;
		
		// Get the main container of the JFrame
		mainCotainer = getContentPane();  
		
		// Set text characteristics
		petNameField.setFont(new Font("Serif", Font.PLAIN, 14));
		LongDesctextArea.setFont(new Font("Serif", Font.PLAIN, 14));
		shortDescField.setFont(new Font("Serif", Font.PLAIN, 14));
		deliverUserField.setFont(new Font("Serif", Font.PLAIN, 14));
		
		// Register buttons action listeners
		closeButton.addActionListener(closebuttonHandler);
		
		// Set components as read-only
		petNameField.setEditable(false);
		LongDesctextArea.setEditable(false);
		shortDescField.setEditable(false);
		deliverUserField.setEditable(false);
		petTypeField.setEditable(false);
		
		// Set the vertical scrollbar display for the text area
		longDescScrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		// Set pet's picture button size
		pictureButton.setSize(PICTURE_WIDTH, PICTURE_HEIGHT);
		picturePane.setSize(PICTURE_WIDTH, PICTURE_HEIGHT);
		
		// add components to pans and to main container
		picturePane.add(pictureButton, BorderLayout.CENTER);
		addtextRows(labels, textFields, textControlsPane);
		buttonsPanel.add(closeButton);
		mainCotainer.add(textControlsPane,BorderLayout.CENTER);
		mainCotainer.add(buttonsPanel,BorderLayout.SOUTH);
		
		// Disable window's resizing
		setResizable(false);
		
		// if user press on X button then call the close button handler
		addWindowListener(new WindowAdapter(){
			public void windowClosing (WindowEvent e){
				closebuttonHandler.actionPerformed(null);
			}
		});
	}

	// Methods Definition
	public void displayPetDetailsFrame (IPet pet)
	{
		/** This method is called from the client frame and is responsible for displaying
		 * the selected pet's details. It receive the selected pet as a parameter. retrieve
		 * its data members and displays them. if the pet has a picture the method rescales
		 * the picture size and set it on a button*/
		
		// Update frame fields
		selectedPet = pet;
		user = selectedPet.getDeliver_user();
		petNameField.setText(selectedPet.getName());
		petType = selectedPet.getPet_type();
		deliverUserField.setText(user.getUsername());
		petTypeField.setText(petType.getType_name()); 
		shortDescField.setText(selectedPet.getShort_desc());
		LongDesctextArea.setText(selectedPet.getLong_desc());
		
		picture = selectedPet.getPicture();
		if (picture != null)
		{
			// Set the pciture size 
			petImage = new ImageIcon(picture);
			petImage = getScaledImage(petImage.getImage(), PICTURE_WIDTH, PICTURE_HEIGHT);
			
			// Disable picture button
			pictureButton.setIcon(petImage);
			pictureButton.setDisabledIcon(petImage);
			pictureButton.setSize(petImage.getIconWidth(), petImage.getIconHeight());
		}
		else
		{
			// If there is no picture display text
			pictureButton.setText("No Image");
			pictureButton.setSize(PICTURE_WIDTH, PICTURE_HEIGHT);	
		}
		pictureButton.setMargin(new Insets(-1,-1,-1,-1));
		pictureButton.setEnabled(false);
		
		
		
		// Displaying the frame
		setSize(800,800);
		pack();
		setAlwaysOnTop(true);
		setResizable(false);
		
		// The user can't work with the main client frame until he close this frame
		callingFrame.setFocusable(false);
		callingFrame.setEnabled(false);
		
		// Set the frame position to the center of the screen
		setLocationRelativeTo(null);
		setVisible(true);	
	}
	
	private void addtextRows(JLabel[] labels, JTextField[] textfields, 
			Container container)
	{
		/* the GridBagLayout is compound from 4 rows and 3 columns */
		int index;
		int numofLabels = labels.length;
		Insets insets = new Insets(4,2,2,2);
		GridBagConstraints labelsConstrains = new GridBagConstraints();
		GridBagConstraints textfieldsConstrains = new GridBagConstraints();
		GridBagConstraints textareaConstrains = new GridBagConstraints();
		GridBagConstraints pictureConstrains = new GridBagConstraints();
		
		// set constraints for labels
		labelsConstrains.gridx = 0; // labels at the left column
		labelsConstrains.gridy = 0;
		labelsConstrains.anchor = GridBagConstraints.FIRST_LINE_START; // align label to the uperleft
		labelsConstrains.gridwidth = 1; // each label occupies 1 cell
		labelsConstrains.fill = GridBagConstraints.NONE;
		labelsConstrains.weightx = 0.0;
		labelsConstrains.weighty = 0.0;
		labelsConstrains.insets = insets;
		
		// set constraints for text fields
		textfieldsConstrains.gridx = 1; // text fields at the second column 
		textfieldsConstrains.gridy = 0;
		textfieldsConstrains.anchor = GridBagConstraints.FIRST_LINE_START; // align text fields to the left
		textfieldsConstrains.gridwidth = 1; // each text field occupies 1 cell
		textfieldsConstrains.weightx = 0.5;
		textfieldsConstrains.weighty = 0.0;
		textfieldsConstrains.insets = insets;
		textfieldsConstrains.fill = GridBagConstraints.HORIZONTAL;
		
		// set constraints for text area
		textareaConstrains.gridx = 1; // text area at the second column 
		textareaConstrains.anchor = GridBagConstraints.FIRST_LINE_START;; // align text areas to the left
		textareaConstrains.gridwidth = 2; // text area occupies 2 cell
		textareaConstrains.weightx = 1.0;
		textareaConstrains.weighty = 1.0;
		textareaConstrains.insets = insets;
		textareaConstrains.fill = GridBagConstraints.HORIZONTAL;
		
		// set constraints for picture pan
		pictureConstrains.gridx = 2; // picture at the third column 
		pictureConstrains.gridy = 0; // picture at the first row
		pictureConstrains.anchor = GridBagConstraints.FIRST_LINE_END; // picture areas to the left
		pictureConstrains.gridwidth = 1; // picture occupies 1 row cell 
		pictureConstrains.gridheight = 4; // picture occupies 4 row cell
		pictureConstrains.weightx = 0.0;
		pictureConstrains.weighty = 0.0;
		pictureConstrains.insets = new Insets(2,2,2,2);
		
		// put components in layout
		for (index = 0; index < (numofLabels - 1); index++)
        {
			// put labels and text fields
			container.add(labels[index], labelsConstrains);
			container.add(textfields[index], textfieldsConstrains);
			labelsConstrains.gridy++;
			textfieldsConstrains.gridy++;
        }
		
		// put the rest of the components
		container.add(picturePane, pictureConstrains);
		container.add(labels[index], labelsConstrains);
		textareaConstrains.gridy = index; // text area at the last row
		container.add(longDescScrollPane, textareaConstrains);
	}
	
	private ImageIcon getScaledImage(Image srcImg, int w, int h)
	{
		/** This method receive an image and resize it to desirable size*/
		
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return new ImageIcon(resizedImg);
    }

	// Inner Classes Definition
	private class closeButtonHandler implements ActionListener {
		public void actionPerformed (ActionEvent e) 
		{
				setVisible(false);
				setAlwaysOnTop(false);
				selectedPet = null;
				pictureButton.setIcon(null);
				pictureButton.setDisabledIcon(null);
				pictureButton.setText(null);
				
				// Enable the calling frame end exit
				callingFrame.setFocusable(true);
				callingFrame.setVisible(true);
				callingFrame.setEnabled(true);
				dispose();
		}
	}
}
